/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.is.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.is.dto.IsExamDetailsDTO;
import com.pig4cloud.pigx.is.entity.IsExamDetails;
import com.pig4cloud.pigx.is.mapper.IsExamDetailsMapper;
import com.pig4cloud.pigx.is.po.IsExamDetailsPO;
import com.pig4cloud.pigx.is.service.IsExamDetailsService;
import com.pig4cloud.pigx.is.vo.IsExamDetailsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 考场学生明细信息
 *
 * @author skyorange
 * @date 2020-11-05 21:28:33
 */
@Service
public class IsExamDetailsServiceImpl extends ServiceImpl<IsExamDetailsMapper, IsExamDetails> implements IsExamDetailsService {

	@Autowired
	private IsExamDetailsMapper isExamDetailsMapper;

	@Override
	public Page<IsExamDetailsVO> getPage(Page page, IsExamDetailsDTO isExamDetailsdto) {
		Page p = isExamDetailsMapper.selectPage(page, isExamDetailsdto);
		List<IsExamDetailsPO> records = p.getRecords();
		List<IsExamDetailsVO> list = new ArrayList();
		LocalDateTime now = isExamDetailsMapper.getNowTime();
		for (IsExamDetailsPO record : records) {
			IsExamDetailsVO v = BeanUtil.toBean(record, IsExamDetailsVO.class);
			//考试开始且学生已登录客户端
			if (now.compareTo(record.getExamStartTime()) >= 0 && record.getStudentStartTime() != null) {
				long sum;
				if (record.getStudentEndTime() == null) {
					sum = Duration.between(record.getExamStartTime(), record.getRealTime()).toMinutes();
				} else {
					sum = Duration.between(record.getExamStartTime(), record.getStudentEndTime()).toMinutes();
				}
				double tmp = sum != 0 ? record.getCumulateTime() * 100.00 / sum : 0.00;
				if (tmp > 100.00) {
					tmp = 100.00;
				}
				String proportion = String.format("%.1f", tmp);
				long studentExitClientTime;
				if (now.compareTo(record.getExamEndTime()) < 0) {
					studentExitClientTime = Duration.between(record.getRealTime(), now).toMinutes();
				} else {
					studentExitClientTime = Duration.between(record.getRealTime(), record.getExamEndTime()).toMinutes();
				}
				if (studentExitClientTime <= 2) {
					studentExitClientTime = 0;
				}
				v.setExamTime(sum);
				v.setProportion(proportion);
				v.setStudentExitClientTime(studentExitClientTime);

			} else {
				v.setExamTime(0L);
				v.setProportion("0.0");
				v.setStudentExitClientTime(0L);
			}
			list.add(v);
		}
		Page<IsExamDetailsVO> newP = p.setRecords(list);
		return newP;
	}
}
